package services;

import dto.BrandDto;
import models.Brand;
import play.i18n.Lang;

import java.util.List;

/**
 * Created by Gipxy group
 * Date: 3/17/12
 */
public class BrandService {

    public static BrandDto createDto(Brand model) {
        BrandDto dto = new BrandDto();
        dto.id = model.id;
        dto.homepage = model.homepage;
        dto.title = model.title;
        dto.order = model.order;
        dto.language = model.language;
        dto.photoAsBytes = model.logo;
        return dto;
    }

    //check exits: language + code
    public static boolean isExisted(String code, String language) {
        List<Brand> fetch = Brand.all(Brand.class).filter("code", code).filter("language", language).fetch();
        return fetch.size()>0;
    }

    public static Brand merge(BrandDto dto) {
        Brand merged=null;
        if (dto.id!=null) {
            merged = Brand.getByKey(Brand.class,dto.id);
        }
        if (merged==null){
            merged = new Brand();
        }

        merged.title = dto.title;
        merged.homepage = dto.homepage;
        merged.order = dto.order;
        merged.language = Lang.get();

        if (dto.photo!=null)
            merged.logo = dto.photo.asBytes();

        merged.save();

        return merged;
    }
}
